package realms;

import dao.NurseDao;
import dao.NurseRoleDao;
import dao.PermissionDao;
import dao.RolePermissionDao;
import entity.Nurse;
import entity.NurseRole;
import entity.Permission;
import entity.RolePermission;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("myRealm")
public class MyRealm extends AuthorizingRealm {
    @Autowired
    private NurseDao ndao;

    /**
     * 认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        SimpleAuthenticationInfo info=null;
        UsernamePasswordToken token=(UsernamePasswordToken) authenticationToken;
        Nurse nurse=ndao.login(token.getUsername());
        Subject subject = SecurityUtils.getSubject();

        Session session = subject.getSession();
        if (nurse==null){
            throw new UnknownAccountException("账号不正确");
        }else{
            session.setAttribute("nurse",nurse);
            info=new SimpleAuthenticationInfo(nurse.getnCall(),nurse.getnPwd(),"xx");
        }
            return info;
    }
    /**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }
}
